package com.tl.datashare.exception;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author fyy
 * @Date 2022/07/25
 */
@RestControllerAdvice
public class ExptAdvice {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ExptBean defaultException(HttpServletRequest request, Exception e){
        return ExptBean.builder()
                .code(ExptEnum.EXCEPTION.getCode())
                .message(ExptEnum.EXCEPTION.getMessage())
                .build();
    }

    @ExceptionHandler(value = RequestExeception.class)
    @ResponseBody
    public ExptBean myException(HttpServletRequest request,RequestExeception e){
        Integer code = e.getCode();
        String message = e.getMessage();

        if(null == e.getCode()){
            code = ExptEnum.EXCEPTION.getCode();
        }

        if (null == e.getMessage()){
            message = ExptEnum.EXCEPTION.getMessage();
        }

        return ExptBean.builder()
                .code(code)
                .message(message)
                .build();
    }

}
